package com.example.myapplication.SQLite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    String sql_version_2="alter table user add column age integer";///增加列 age 类型 integer

    public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        ///创建数据库
        String sql = "CREATE TABLE user(userid integer primary key autoincrement,"+"username text not null,"+"userpwd text not null)";

        sqLiteDatabase.execSQL(sql);


        ///创建触发器
        /*sqLiteDatabase.execSQL(sql);*/
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        ///对数据库修改增加表，修改表结构
        if(oldVersion<2){
            db.execSQL(sql_version_2);
        }

        ///激活触发器
        /*if(!db.isReadOnly()){
            db.execSQL("PRAGMA foreign_keys=ON;");
        }*/
    }
}
